//
//  TiDanChooseCarVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/14.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "TiDanChooseCarVC.h"
#import "TiDanChooseCarModel.h"
#import "TiDanChooseCarCell.h"
#import "JieDanSuccessVC.h"

@interface TiDanChooseCarVC () <UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <TiDanChooseCarModel *>*array;

@end

@implementation TiDanChooseCarVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - NaviHeight - 64 - BottomHeight, ScreenW, 64 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, ScreenW - 60, 44)];
        [btn setTitleColor:HexColor(0xffffff) forState:0];
        btn.titleLabel.font = BoldFontOfSize(16);
        [btn setTitle:@"确认提交" forState:0];
        btn.backgroundColor = Color_ThemeColor;
        btn.layer.cornerRadius = 6;
        [btn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:btn];
    }
    return _bottomView;
}

- (void)commitClick {
    __block BOOL isCan = NO;
    __block NSString *v_id = @"";
    [self.array enumerateObjectsUsingBlock:^(TiDanChooseCarModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            isCan = YES;
            v_id = obj.v_id;
        }
    }];
    if (!isCan) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择车辆"];
        return;
    }
    [HttpRequest PostWithApi:self.isModify ? LogisticsSupplierTiDanManagerModifyCar : LogisticsSupplierTiDanManagerJieDan params:@{@"sdl_id":self.sdl_id,@"v_id":v_id,@"sdlv_id":self.sdlv_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self.isModify) {
                __weak_self;
                [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"修改成功" delayOperation:^{
                    [weakself.rt_navigationController popViewControllerAnimated:YES];
                }];
            }else {
                [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshTiDanShouLiList object:nil];
                __weak_self;
                [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"接单成功" delayOperation:^{
                    [weakself.rt_navigationController removeViewController:weakself];
                    JieDanSuccessVC *vc = [JieDanSuccessVC new];
                    [weakself.rt_navigationController pushViewController:vc animated:YES];
                }];
            }
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

-(NSMutableArray<TiDanChooseCarModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择提货车辆";
    page = 1;
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:LogisticsSupplierTiDanManagerCarList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [TiDanChooseCarModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [TiDanChooseCarModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TiDanChooseCarCell *cell = [TiDanChooseCarCell TiDanChooseCarCellWithTableView:tableView];
    TiDanChooseCarModel *model = self.array[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TiDanChooseCarCell *cell = [TiDanChooseCarCell TiDanChooseCarCellWithTableView:tableView];
    TiDanChooseCarModel *model = self.array[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.array enumerateObjectsUsingBlock:^(TiDanChooseCarModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.row) {
            obj.isSelected = !obj.isSelected;
        }else {
            obj.isSelected = NO;
        }
    }];
    [tableView reloadData];
}

@end
